package org.komarichin.struts2.actions.partition;

import org.apache.commons.lang.StringUtils;
import org.komarichin.beans.ILanguage;
import org.komarichin.beans.IPartition;
import org.komarichin.beans.service.IPartitionService;
import org.komarichin.struts2.BaseAction;
import org.komarichin.struts2.beans.models.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;

/**
 * User: Mykhaylo Komarichyn
 * Date: 22.12.2009
 * Time: 15:04:14
 */
public class PartitionAction extends BaseAction {
    private static final long serialVersionUID = -7539394265749610922L;
    private static final Logger log = LoggerFactory.getLogger(PartitionAction.class);

    @Autowired
    private IPartitionService<Long> partitionService;
    @Autowired
    private IPartition<Long> partition;

    private PageModel page = new PageModel();


    public String save() {
        log.debug("save: {}", partition);
        partition = partitionService.save(partition);
        if (partition.getId() != null) {
            addActionMessage(getText("message.create.success"));
        } else {
            addActionError(getText("error.not.created"));
        }
        return SUCCESS;
    }

    public String update() {
        log.debug("update: {}", partition);
        partitionService.saveOrUpdate(partition);
        return SUCCESS;
    }

    public String load() {
        log.debug("loading partition# {}", partition);
        if (partition != null && partition.getId() != null) {
            this.partition = partitionService.findById(partition.getId());
            defineMetaTags(this.partition);
            return SUCCESS;
        } else {
            log.warn("user not choice partition id");
            addActionError(getText("partition.error.not.choice.partition"));
            return INPUT;
        }
    }

    public String delete() {
        log.debug("delete: {}", partition);
        if (partition != null && partition.getId() != null) {
            partitionService.remove(partition.getId());
            addActionMessage(getText("message.delete.success"));
            return SUCCESS;
        } else {
            log.warn("user not choice partition id");
            addActionError(getText("error.not.delete"));
            return INPUT;
        }
    }

    public Collection<IPartition<Long>> getPartitionList() {
        log.debug("get partition list#page: " + page);
        if (StringUtils.isNotEmpty(getUserLanguage())) {
            ILanguage lang = getLanguageService().findByName(getUserLanguage());
            if (lang != null) {
                return partitionService.getByCriteries(lang, page.getStart(), page.getLimit(), page.getField(), page.isOrder());
            }
        }
        return partitionService.getByCriteries(page.getStart(), page.getLimit(), page.getField(), page.isOrder());
    }

    public IPartition getPartition() {
        return partition;
    }


    public Collection<IPartition<Long>> getAllPartition() {
        return partitionService.getAll();
    }

    public PageModel getPage() {
        return page;
    }

    public void setPage(PageModel page) {
        this.page = page;
    }
}
